<?php
/**
 * 购物车逻辑处理
 * User:LLW
 * Date:2020-03-30
 */

namespace app\api\model;

use think\Db;
use think\Model;
use think\Session;

class OrderCartModel extends Model
{
    protected $table = 'yc_order_cart';

    /**
     * 购物车首页
     * @return object
     */
    public function getList()
    {
        $uid = Session::get('uid');
        $list = $this->where('oc_uid',$uid)->select();
        foreach ($list as $v){
            $good = Db::name('goods')->alias('g')
                ->join('yc_category c','g.go_caid=c.ca_id')
                ->where('go_id',$v['oc_gid'])
                ->field('g.go_name,g.go_thumb,g.go_price,g.go_weight,c.ca_parent')->find();
            $good['nums'] = $v['oc_num'];
            $good['id'] = $v['oc_id'];
            if($good['ca_parent'] == 1){
                $arr['Te'][] = $good;
            }else{
                $arr['Qx'][] = $good;
            }
        }
        AjaxReturn(1,'ok',$arr);
    }

    /**
     * 购物车商品的操作
     * @param $id 购物车id
     * @param $type 1:删除 2:添加或者减少数量
     * @param $num 所要改变的数值
     * @return object
     */
    public function getState()
    {
        $id = input('post.id',null);
        $type = input('post.type',null);
        $num = input('post.num',0);
        $find = $this->where('oc_id',$id)->find();
        if(empty($id)){
            AjaxReturn(-1,'数据获取失败');
        }
        if($type == 1){
            $res = $this->where('oc_id',$id)->delete();
        }else{
            if($find['oc_num'] + $num <= 0){
                AjaxReturn(-1,'哎呀，商品数量已经到底线了哦');
            }
            $res = $this->where('oc_id',$id)->setInc('oc_num',$num);
        }
        if($res){
            AjaxReturn(1,'ok');
        }else{
            AjaxReturn(-1,'请您操作慢些，小青快跟不上了');
        }
    }
}
